package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.AccountItemEntity;
import com.tfjybj.itoo.exam.model.AccountItemModel;
import com.tfjybj.itoo.exam.model.AddAccountItemModel;
import com.tfjybj.itoo.exam.provider.service.AccountItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * AccountItemController
 * accountItem表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"accountItem表接口"})
@RequestMapping(value = "/accountItem")
@RestController
@Slf4j
public class AccountItemController {

    @Resource
    private AccountItemService accountItemService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model AccountItemModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody AccountItemModel model) {
		if (StringUtils.isEmpty(model.getTitle())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        AccountItemEntity accountItemEntity = new AccountItemEntity();
        BeanUtils.copyProperties(model, accountItemEntity);
        accountItemService.save(accountItemEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        accountItemService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        accountItemService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model AccountItemModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改accountItem")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody AccountItemModel model) {
		if (StringUtils.isEmpty(model.getTitle())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        AccountItemEntity accountItemEntity = new AccountItemEntity();
        BeanUtils.copyProperties(model, accountItemEntity);
        accountItemService.updateById(accountItemEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找AccountItem
     *
     * @param id 主键id
     * @author 任晓帅
     * @return 查找的结果
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        AccountItemEntity accountItemEntity = accountItemService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", accountItemEntity);
    }

    /**
     * 分页查询所有AccountItem
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @return 查找的结果
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有AccountItem")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize) {
        PageInfo<AccountItemEntity> accountItems = accountItemService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", accountItems);
    }
	
	/**
     * 根据名称模糊查询accountItem
     *
     * @param name  名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的accountItem
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据名称模糊查询accountItem", notes = "分页根据名称模糊查询accountItem")
    @GetMapping(value = "queryAccountItemByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryAccountItemByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                                 @ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
                                                 @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize) {
        PageInfo<AccountItemEntity> accountItemList = accountItemService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", accountItemList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 查询会计科目
     *
     * @param name  科目名称
     * @return 会计科目
     * @author 杨楠
     * @since 1.0.0 2018年11月5日15:39:54
     */

    @ApiOperation(value = "查询会计科目")
    @GetMapping(value = {"/queryAccountingSubject/{name}"})
    public ItooResult queryAccountingSubject (@ApiParam(value = "科目名称",required = true) @PathVariable String name){

        List<AccountItemModel> list = accountItemService.queryAccountingSubject(name);
        if (list !=null && !list.isEmpty()){
            return  ItooResult.build(ItooResult.SUCCESS,"查询会计科目成功",list);
        }else{
            return ItooResult.build(ItooResult.FAIL,"查询会计科目失败");
        }
    }


    /**
     * excel导入会计科目
     *
     *
     * @param multipartFile  Excel文件
     * @return 会计科目
     * @author 郭凯
     * @since 1.0.0 2018年11月5日15:39:54
     *
     */
    @ApiOperation(value = "Excel导入会计分录",notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importByExcel"})
    public ItooResult importByExcel(@RequestParam(value = "file") MultipartFile multipartFile,
                                    HttpServletRequest request, HttpServletResponse response,
                                    @ApiParam(name = "operator",value = "教师id",required = true) @RequestParam(value = "operator") String operator){
        return accountItemService.importByExcel(multipartFile,request,response,operator);
    }


    /**
     * 查询accountItemModel的树形结构
     *
     *
     * @return treeModel
     * @author 郭凯
     * @since 2.0.0 2019-1-24 19:52:083
     *
     */
    @ApiOperation("查询accountItemModel的树形结构")
    @GetMapping(value = {"/queryTreeModel"})
    public ItooResult queryTreeModel(){
        List<AccountItemEntity> accountItemEntities = accountItemService.queryTreeModel();
        if (accountItemEntities.size() > 0){
            return ItooResult.build(ItooResult.SUCCESS,"查询成功",accountItemEntities);
        }
        else {
            return ItooResult.build(ItooResult.FAIL,"暂无数据");
        }
    }

    /**
     * 修改
     *
     * @param model AddAccountItemModel
     * @return 修改的结果
     * @author 郭满亮
     * @since 2019年4月30日14:42:54
     */
    @ApiOperation(value = "修改")
    @PutMapping(value = {"/modifyAccountItemModel"})
    public ItooResult modifyAccountItemModel(@RequestBody AddAccountItemModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        AccountItemEntity accountItemEntity = new AccountItemEntity();
        accountItemEntity.setId(model.getKey());
        BeanUtils.copyProperties(model, accountItemEntity);
        accountItemService.updateById(accountItemEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }


    /**
     * 添加
     *
     * @param models AccountItemModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/createAccountItemModel"})
    public ItooResult createAccountItemModel(@RequestBody List<AddAccountItemModel> models) {
        if(accountItemService.createAccount(models)){
            return ItooResult.build(ItooResult.SUCCESS,"添加成功");
        }
        else{
            return ItooResult.build(ItooResult.FAIL,"添加失败，请重试");
        }
    }

    /**
     *
     * @param AddAccountItemModel
     * @return
     * @author 杜雨
     * @since 2.0.0 2019年6月19日10:57:57
     */
    @ApiOperation("添加createAccountItemNode的树节点")
    @PostMapping(value = {"/createAccountItemNode"})
    public ItooResult createAccountItemNode(@RequestBody AddAccountItemModel AddAccountItemModel) {
        if(AddAccountItemModel==null){
            log.info("添加createAccountItemNode的树节点 参数AddAccountItemModel为空！");
            return ItooResult.build(ItooResult.FAIL, "分类为空！");
        }
        AccountItemEntity addAccountItemaEntity = accountItemService.createAccountItemNode(AddAccountItemModel);
        if (addAccountItemaEntity !=null && !StringUtils.isEmpty(addAccountItemaEntity.getId())) {
            return ItooResult.build(ItooResult.SUCCESS,"添加成功", addAccountItemaEntity.getId());
        } else {
            return ItooResult.build(ItooResult.FAIL, "添加失败");
        }
    }

}    
